package chu.dejian.dialogOrMenu;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import org.achartengine.chart.AbstractChart;

import java.text.SimpleDateFormat;
import java.util.List;

import com.sam.blemodule.R;
import chu.dejian.entity.RecordEntity;
import chu.dejian.widget.chartView.BPChartView;
import com.sam.widget.headerBar.BaseHeaderView;
import com.sam.widget.headerBar.LeftImgTitleRightImgBar;

public class DayRecordsDialog extends Dialog {
	private Context context;
	private LeftImgTitleRightImgBar header;
	private BPChartView dayChart;
	private TextView shrinkageValueTV,diastolicValueTV,pulseValueTV,recordTime;
	private View rightArrow,leftArrow;
	private int curRecordIndex = 0;
	private List<RecordEntity> dayRecordEntities;

	public DayRecordsDialog(Context context) {
		super(context,R.style.DialogTheme);
		this.context = context;
		setContentView(R.layout.dialog_day_records); // 最好放在这两句中间
		setCanceledOnTouchOutside(true);// 点击对话框外部取消对话框显示
		WindowManager.LayoutParams lp = getWindow().getAttributes();
//		getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);// 添加模糊效果 ---- 不但没有模糊效果，窗口四个角还有黑色，fuck
		// 设置透明度，对话框透明(包括对话框中的内容)alpha在0.0f到1.0f之间。1.0完全不透明，0.0f完全透明
//		lp.alpha = 1.0f;
		lp.dimAmount = 0.5f;// 设置对话框显示时的黑暗度，0.0f和1.0f之间，在我这里设置成0.0f会出现黑屏状态
		getWindow().setAttributes(lp);

		findView();
		initView();
	}

	public void findView() {
		header = (LeftImgTitleRightImgBar) findViewById(R.id.dialog_header);
		dayChart = (BPChartView) findViewById(R.id.day_chart);
		leftArrow = findViewById(R.id.left_arrow);
		rightArrow = findViewById(R.id.right_arrow);
		shrinkageValueTV = (TextView) findViewById(R.id.shrinkage_value);
		diastolicValueTV = (TextView) findViewById(R.id.diastolic_value);
		pulseValueTV = (TextView) findViewById(R.id.pulse_value);
		recordTime = (TextView) findViewById(R.id.record_time);
	}

	public void initView() {
		header.setOnHeaderClickListener(onHeaderClickListener);
		leftArrow.setOnClickListener(onClickListener);
		rightArrow.setOnClickListener(onClickListener);
	}

	public void setDayRecords(List<RecordEntity> records){
		dayRecordEntities = records;
		dayChart.setRecordEntityList(dayRecordEntities, BPChartView.DISPLAY_TYPE.DIALOG);
		dayChart.setLineColor(context.getResources().getColor(R.color.main_blue_color));
		if(dayRecordEntities == null || dayRecordEntities.size() == 0) return;
		viewRecord(dayRecordEntities.get(0));

		dayChart.setRecordPointClickListener(new BPChartView.RecordPointClickListener() {
			@Override
			public void onRecordPointClicked(RecordEntity recordEntity, int pointIndex, int pointValue) {
				curRecordIndex = pointIndex;
				viewRecord(recordEntity);
				checkArrowState();
			}
		});
		dayChart.setOnChartItemClickListener(new AbstractChart.ChartItemClickListener() {
			@Override
			public void onChartItemClick(double position) {
				curRecordIndex = (int)position;
				if(curRecordIndex >= 0 && curRecordIndex < dayRecordEntities.size())
					viewRecord(dayRecordEntities.get((int)position));
				checkArrowState();
			}
		});
	}

	private void checkArrowState(){
		if(dayRecordEntities.size() < 2) {
			leftArrow.setVisibility(View.INVISIBLE);
			rightArrow.setVisibility(View.INVISIBLE);
		} else {
			leftArrow.setVisibility(View.VISIBLE);
			rightArrow.setVisibility(View.VISIBLE);
			if(curRecordIndex == 0) {
				leftArrow.setVisibility(View.INVISIBLE);
			}
			if(curRecordIndex == dayRecordEntities.size() - 1){
				rightArrow.setVisibility(View.INVISIBLE);
			}
		}

	}

	private View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			int i = v.getId();
			if (i == R.id.left_arrow) {
				if (dayRecordEntities == null || dayRecordEntities.size() == 0) return;
				dayChart.focusePreviousItem();
				curRecordIndex--;
				checkArrowState();

			} else if (i == R.id.right_arrow) {
				if (dayRecordEntities == null || dayRecordEntities.size() == 0) return;
				dayChart.focuseNextRecord();
				curRecordIndex++;
				checkArrowState();

			}
		}
	};


	private void viewRecord(RecordEntity recordEntity){
		if(recordEntity == null) {
			recordTime.setText(R.string.no_record_data);
			shrinkageValueTV.setText("-");
			diastolicValueTV.setText("-");
			pulseValueTV.setText("-");
		} else {
			recordTime.setText(new SimpleDateFormat(context.getResources().getString(R.string.message_time_format)).format(recordEntity.getMeasureDateTime()));
			shrinkageValueTV.setText(recordEntity.systolic+"");
			diastolicValueTV.setText(recordEntity.diastolic+"");
			pulseValueTV.setText(recordEntity.pulse +"");
		}
	}

	private BaseHeaderView.OnHeaderClickListener onHeaderClickListener = new BaseHeaderView.OnHeaderClickListener() {
		@Override
		public void onHeaderLeftClicked() {
			dismiss();
		}

		@Override
		public void onHeaderRightClicked() {

		}
	};
}
